using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Collections;

using EntLibEC.Cmn;
using EntLibEC.Info.Online;

namespace EntLibEC.Dac.Online
{
	/// <summary>
	/// Summary description for AdvQuestionDac.
	/// </summary>
	public class AdvQuestionDac
	{
		
		public AdvQuestionDac()
		{
			
		}
		public int Insert(AdvQuestionInfo oParam)
		{
			string sql = @"INSERT INTO Ent_Adv_Question
                            (
                            ActivityName, CreateUserSysNo, StartTime, 
                            EndTime
                            )
                            VALUES (
                            @ActivityName, @CreateUserSysNo, @StartTime, 
                            @EndTime
                            )";			
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramActivityName = new SqlParameter("@ActivityName", SqlDbType.NVarChar,100);
			SqlParameter paramCreateUserSysNo = new SqlParameter("@CreateUserSysNo", SqlDbType.Int,4);
			SqlParameter paramStartTime = new SqlParameter("@StartTime", SqlDbType.DateTime);
			SqlParameter paramEndTime = new SqlParameter("@EndTime", SqlDbType.DateTime);

			if ( oParam.SysNo != AppConst.IntNull)
				paramSysNo.Value = oParam.SysNo;
			else
				paramSysNo.Value = System.DBNull.Value;
			if ( oParam.ActivityName != AppConst.StringNull)
				paramActivityName.Value = oParam.ActivityName;
			else
				paramActivityName.Value = System.DBNull.Value;
			if ( oParam.CreateUserSysNo != AppConst.IntNull)
				paramCreateUserSysNo.Value = oParam.CreateUserSysNo;
			else
				paramCreateUserSysNo.Value = System.DBNull.Value;
			if ( oParam.StartTime != AppConst.DateTimeNull)
				paramStartTime.Value = oParam.StartTime;
			else
				paramStartTime.Value = System.DBNull.Value;
			if ( oParam.EndTime != AppConst.DateTimeNull)
				paramEndTime.Value = oParam.EndTime;
			else
				paramEndTime.Value = System.DBNull.Value;

			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramActivityName);
			cmd.Parameters.Add(paramCreateUserSysNo);
			cmd.Parameters.Add(paramStartTime);
			cmd.Parameters.Add(paramEndTime);

			return SqlHelper.ExecuteNonQuery(cmd);
		}

		public int Update(Hashtable paramHash)
		{
			StringBuilder sb = new StringBuilder(200);
			sb.Append("UPDATE Ent_Adv_Question SET ");

			if ( paramHash != null && paramHash.Count != 0 )
			{
				int index = 0;
				foreach(string key in paramHash.Keys)
				{
					object item = paramHash[key];
					if ( key.ToLower()=="sysno" )
						continue;

					if ( index != 0 )
						sb.Append(",");
					index++;

					
					if (item is int || item is decimal)
					{
						sb.Append(key).Append("=").Append(item.ToString());
					}
					else if (item is string)
					{
						sb.Append(key).Append(" = ").Append(Util.ToSqlString(item.ToString()));
					}
					else if (item is DateTime)
					{
						sb.Append(key).Append(" = cast(").Append( Util.ToSqlString(((DateTime)item).ToString(AppConst.DateFormatLong))).Append(" as DateTime)");
					}
				}
			}

			sb.Append(" WHERE SysNo=").Append(paramHash["SysNo"]);

			return SqlHelper.ExecuteNonQuery(sb.ToString());
		}

		public int Delete(int sysno)
		{
		    string sql = "delete from Ent_Adv_Question where sysno =" + sysno;
			SqlCommand cmd = new SqlCommand(sql);

			return SqlHelper.ExecuteNonQuery(cmd);			

		}
	}
}
